package xijia.plus.docs.manage.all.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.utils.BeanDtoVoUtil;
import io.github.wslxm.springbootplus2.core.utils.tree.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import xijia.plus.docs.constant.CacheKey;
import xijia.plus.docs.manage.all.mapper.DocMenuMapper;
import xijia.plus.docs.manage.all.model.dto.DocMenuDTO;
import xijia.plus.docs.manage.all.model.entity.DocMenu;
import xijia.plus.docs.manage.all.model.query.DocMenuQuery;
import xijia.plus.docs.manage.all.model.vo.DocMenuVO;
import xijia.plus.docs.manage.all.service.DocMenuService;

import java.util.List;

/**
 * 菜单 ServiceImpl
 *
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>
 *
 * @author ws
 * @email 1720696548@qq.com
 * @date 2022-12-29 16:56:28
 */
@Service
//@DS("#header.tenantName")
public class DocMenuServiceImpl extends BaseServiceImpl<DocMenuMapper, DocMenu> implements DocMenuService {


    @Override
    @Cacheable(value =  CacheKey.XJ_DOC_MENU,key = "#tenantName")
    @DS("#tenantName")
    public List<DocMenuVO> tree(String tenantName ,DocMenuQuery query) {
        LambdaQueryWrapper<DocMenu> queryWrapper = new LambdaQueryWrapper<DocMenu>()
                .eq(query.getDisable() != null, DocMenu::getDisable, query.getDisable())
                .orderByAsc(DocMenu::getSort)
                .orderByAsc(DocMenu::getCreateTime);
        List<DocMenu> list = this.list(queryWrapper);
        List<DocMenuVO> listVo = BeanDtoVoUtil.listVo(list, DocMenuVO.class);
        return TreeUtil.nextTree(listVo, "0").getChildren();
    }


    @Override
    @DS("#tenantName")
    public BasePage<DocMenuVO> findPage(String tenantName ,DocMenuQuery query) {
        LambdaQueryWrapper<DocMenu> queryWrapper = new LambdaQueryWrapper<DocMenu>().orderByAsc(DocMenu::getSort)
                .orderByAsc(DocMenu::getCreateTime);
        queryWrapper.likeRight(StringUtils.isNotBlank(query.getPid()), DocMenu::getPid, query.getPid());
        queryWrapper.likeRight(StringUtils.isNotBlank(query.getName()), DocMenu::getName, query.getName());
        queryWrapper.eq(query.getDisable() != null, DocMenu::getDisable, query.getDisable());
        Page<DocMenu> page = this.page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);
        return BeanDtoVoUtil.pageVo(page, DocMenuVO.class);
    }

    @Override
    @DS("#tenantName")
    public DocMenuVO findId(String tenantName ,String id) {
        return BeanDtoVoUtil.convert(this.getById(id), DocMenuVO.class);
    }

    @Override
    @CacheEvict(value =  CacheKey.XJ_DOC_MENU,key = "#tenantName", allEntries = true)
    @DS("#tenantName")
    public String insert(String tenantName ,DocMenuDTO dto) {
        DocMenu entity = dto.convert(DocMenu.class);
        boolean b = this.save(entity);
        return entity.getId();
    }

    @Override
    @CacheEvict(value = CacheKey.XJ_DOC_MENU,key = "#tenantName", allEntries = true)
    @DS("#tenantName")
    public boolean upd(String tenantName ,String id, DocMenuDTO dto) {
        DocMenu entity = dto.convert(DocMenu.class);
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    @CacheEvict(value =  CacheKey.XJ_DOC_MENU,key = "#tenantName", allEntries = true)
    @DS("#tenantName")
    public boolean del(String tenantName ,String id) {
        List<DocMenu> list = this.list(new LambdaQueryWrapper<DocMenu>().select(DocMenu::getId, DocMenu::getPid));
        List<DocMenuVO> listVos = BeanDtoVoUtil.listVo(list, DocMenuVO.class);
        List<String> nextIds = TreeUtil.nextIds(listVos, id);
        nextIds.add(id);
        return this.removeBatchByIds(nextIds);
    }

}
